package com.cjw.service.impl.livesquare;

import com.cjw.mapper.database.UserMapper;
import com.cjw.mapper.livesquare.CommentLiveParentMapper;
import com.cjw.mapper.livesquare.CommentLiveSonMapper;
import com.cjw.mapper.person.UserDetailMapper;
import com.cjw.pojo.dto.database.UserDTO;
import com.cjw.pojo.dto.livesquare.LiveCommentDTO;
import com.cjw.pojo.entity.database.User;
import com.cjw.pojo.entity.livesquare.CommentParent;
import com.cjw.pojo.entity.livesquare.CommentSon;
import com.cjw.pojo.entity.person.UserDetail;
import com.cjw.pojo.query.livesquare.AddParentComment;
import com.cjw.pojo.query.livesquare.AddSonLiveComment;
import com.cjw.service.livesquare.CommentService;
import com.cjw.util.BaseContextHolder;
import com.cjw.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @create_time: 2021.12.4
 * @author : cjw
 */
@Service("commentLiveService")
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentLiveParentMapper parentMapper;

    @Autowired
    private CommentLiveSonMapper sonMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDetailMapper userDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addParentComment(AddParentComment comment) {
        CommentParent copy = BeanUtils.copy(comment, CommentParent.class);
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        copy.setUserId(user.getId());
        copy.setAddTime(new Date());
        return parentMapper.insert(copy)==1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addSonComment(AddSonLiveComment comment) {
        CommentSon copy = BeanUtils.copy(comment, CommentSon.class);
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        copy.setAddTime(new Date());
        copy.setUserId(user.getId());
        copy.setResponseId(comment.getResponseId());
        return sonMapper.insert(copy)==1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteComment(Long id, Integer type) {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        int delete = 0;
        // 父评论
        if (type == 0) {
            CommentParent commentLiveParent = new CommentParent();
            commentLiveParent.setUserId(user.getId());
            commentLiveParent.setId(id);
            delete = parentMapper.delete(commentLiveParent);
        } // 子评论
        else if (type == 1) {
            CommentSon commentLiveSon = new CommentSon();
            commentLiveSon.setId(id);
            commentLiveSon.setUserId(1L);
            delete = sonMapper.delete(commentLiveSon);
        }
        return delete == 1;
    }

    @Override
    public List<List<LiveCommentDTO>> getAllComment(Long id, Integer type) {
        List<List<LiveCommentDTO>> dtos = new ArrayList<>();
        // 查询父评论
        CommentParent commentLiveParent = new CommentParent();
        commentLiveParent.setLiveId(id);
        commentLiveParent.setType(type);
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        for (CommentParent parent : parentMapper.select(commentLiveParent)) {
            ArrayList<LiveCommentDTO> liveCommentDTOS = new ArrayList<>();
            liveCommentDTOS.add(parentCommentToDTO(parent, user.getId()));
            // 查询子评论
            CommentSon commentLiveSon = new CommentSon();
            commentLiveSon.setParentId(parent.getId());
            List<CommentSon> sons = sonMapper.select(commentLiveSon);
            for (CommentSon son : sons) {
                LiveCommentDTO liveCommentDTO = sonCommentToDTO(son, user.getId());
                liveCommentDTOS.add(liveCommentDTO);
            }
            dtos.add(liveCommentDTOS);
        }
        return dtos;
    }

    @Override
    public Integer getCommentNum(Long id, Integer type) {
        CommentParent parent = new CommentParent();
        parent.setLiveId(id);
        parent.setType(type);
        List<CommentParent> parentComment = parentMapper.select(parent);
        if (parentComment == null)
            return 0;
        Integer commentNum = parentComment.size();
        for (CommentParent commentLiveParent : parentComment) {
            CommentSon son = new CommentSon();
            son.setParentId(commentLiveParent.getId());
            List<CommentSon> select = sonMapper.select(son);
            if (select != null)
                commentNum += select.size();
        }
        return commentNum;
    }

    /**
     * 父评论转换为DTO
     * @param commentLiveParent
     * @param userId
     * @return
     */
    private LiveCommentDTO parentCommentToDTO(CommentParent commentLiveParent, Long userId) {
        LiveCommentDTO liveCommentDTO = new LiveCommentDTO();
        liveCommentDTO.setId(commentLiveParent.getId());
        liveCommentDTO.setAddTime(commentLiveParent.getAddTime());
        liveCommentDTO.setContext(commentLiveParent.getContext());
        liveCommentDTO.setResponseName(null);
        liveCommentDTO.setIsSelf(commentLiveParent.getUserId() == userId);
        liveCommentDTO.setUserName(userMapper.selectByPrimaryKey(commentLiveParent.getUserId()).getUserName());
        UserDetail userDetail = new UserDetail();
        userDetail.setUserId(commentLiveParent.getUserId());
        liveCommentDTO.setHeadImage(userDetailMapper.selectOne(userDetail).getHeadImage());
        liveCommentDTO.setIsParent(true);
        return liveCommentDTO;
    }

    /**
     * 子评论转换为DTO
     * @param son
     * @param userId
     * @return
     */
    private LiveCommentDTO sonCommentToDTO(CommentSon son, Long userId) {
        LiveCommentDTO liveCommentDTO = new LiveCommentDTO();
        liveCommentDTO.setId(son.getId());
        liveCommentDTO.setContext(son.getContext());
        liveCommentDTO.setAddTime(son.getAddTime());
        liveCommentDTO.setIsSelf(son.getUserId() == userId);
        User user = new User();
        user.setId(son.getUserId());
        liveCommentDTO.setUserName(userMapper.selectByPrimaryKey(user).getUserName());
        if (son.getResponseId() != null) {
            CommentSon son1 = new CommentSon();
            son1.setId(son.getResponseId());
            user.setId(sonMapper.selectOne(son1).getUserId());
            liveCommentDTO.setResponseName(userMapper.selectOne(user).getUserName());
        }
        UserDetail userDetail = new UserDetail();
        userDetail.setUserId(son.getUserId());
        liveCommentDTO.setHeadImage(userDetailMapper.selectOne(userDetail).getHeadImage());
        liveCommentDTO.setIsParent(false);
        return liveCommentDTO;
    }
}
